package com.foreverwin.mes.production.controller;

import com.foreverwin.mes.common.constants.HandleBOConstants;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.production.dto.SfcInWorkDto;
import com.foreverwin.mes.production.dto.SfcQueuedDto;
import com.foreverwin.mes.production.service.SfcExService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author syngna
 * @ 生产执行
 * @since 2019-2-9
 */
@RestController
@RequestMapping("/sfcExs")
public class SfcExController {

    private final Logger logger = LoggerFactory.getLogger(SfcExController.class);

    @Autowired
    private SfcExService sfcExService;

    /**
     * 查询派工可下达SFC
     *
     * @param resrce
     * @return
     */
    @ResponseBody
    @GetMapping("/listReleasableSfc")
    public AjaxResult listReleasableSfc(String resrce) {
        List<SfcQueuedDto> sfcQueuedDtoList;
        String site = CommonMethods.getSite();
        try {
            sfcQueuedDtoList = sfcExService.listReleasableSfc(site, resrce);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(sfcQueuedDtoList);
    }

    /**
     * 查询排队中SFC
     *
     * @param resrce
     * @param operation
     * @return
     */
    @ResponseBody
    @GetMapping("/listQueuedOrReleasableSfc")
    public AjaxResult listQueuedOrReleasableSfc(String resrce, String operation) {
        List<SfcQueuedDto> sfcQueuedDtoList;
        String site = CommonMethods.getSite();
        try {
            sfcQueuedDtoList = sfcExService.listQueuedOrReleasableSfc(site, resrce, operation);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(sfcQueuedDtoList);
    }

    /**
     * 查找工作中生产批
     * @param resrce
     * @param operation 为空则不做条件
     * @param sfc 为空则不做条件
     * @return
     */
    @ResponseBody
    @GetMapping("/listSfcInWork")
    public AjaxResult listSfcInWork(String resrce, String operation, String sfc) {
        List<SfcInWorkDto> sfcInWorkDtoList;
        String site = CommonMethods.getSite();
        try {
            sfcInWorkDtoList = sfcExService.listSfcInWork(site, resrce, operation, sfc);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(sfcInWorkDtoList);
    }

    /**
     * 手动完成sfc
     * @param operation
     * @param resource
     * @param sfc
     * @param qty
     * @param close
     * @return
     */
    @GetMapping("/manualCompleteSfc")
    @ResponseBody
    public AjaxResult manualCompleteSfc(String operation, String resource, String sfc, String qty, String close) {
        List<String> sfcNoList = new ArrayList<>();
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            String resourceBo = StringUtils.genHandle(HandleBOConstants.RESOURCE_BO, site, resource);
            String operationBo = StringUtils.genHandle(HandleBOConstants.OPERATION_BO, site, operation, "#");
            String sfcBo = StringUtils.genHandle(HandleBOConstants.SFC_BO, site, sfc);
            sfcExService.sfcComplete(site, user, resourceBo, operationBo, sfcBo, new BigDecimal(qty), "Y".equalsIgnoreCase(close) ? true : false);
        } catch (Exception e) {
            logger.error("manualCompleteSfc -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(sfcNoList);
    }
}